﻿//* Copyright(C) 2005-2012, all rights reserved.
//*
//* The following code is copyrighted and contains proprietary information
/* and trade secrets of Foxit Software Inc.      
**************************************************************************
*/

/**
* 利用图片显示页面;
*/



define(function (require, exports, module) {
    require('../ViewLevel.js');
    require('../Common.js');
    var Config = require("../Config");
    var CImgLazyLoad = require('../ImageLazyLoad');
    require('../AjaxRetryManager.js');
    require('../Logger.js');
    FXREADER.PDFView.GetImageErrorCode = {
    	ERROR_PAGE_DISPLAY_LIMIT : 1,
    	ERROR_CREATE_IMG_FAILED : 2,
    	ERROR_INPROGRESS : 3
    };

    FXREADER.PDFView.CImgPageViewRender = function (ImgUrlPattern) {
        var _iCurPageNum = -1,
             _PageInRenderList = {},
             _maxRenderPageIndex = -1,
             _self = this;

        this.getMaxRenderedPageIndex = function()
        {
        	return _maxRenderPageIndex;
        };
        
        //public method 
        this.GetPageImgUrl = function (iPageNum, fZoom, bThumb) {
        	var pDocView = FXREADER.AppInstance.GetMainView().GetDocView();
        	if(pDocView.IsFitWidth())
        	{
        		fZoom = 100;
        		/*if(fZoom > 150)
        			fZoom = 200;
        		else if(fZoom >100 && fZoom <=150)
        			fZoom = 150;
        		else if(fZoom >50 && fZoom <=100)
        			fZoom = 100;
        		else
        			fZoom = 50;*/
        	}
        	
            if (bThumb) {
                return Config.Url.GetPageBGImg + "fileid=" + encodeURIComponent(FXREADER.AppInstance.GetFileID()) + "&page=" + iPageNum + "&zoom=thumb";
            }
            else {
                fZoom = 100;
                return Config.Url.GetPageBGImg + "fileid=" + encodeURIComponent(FXREADER.AppInstance.GetFileID()) + "&page=" + iPageNum + "&zoom="+fZoom;
            }
        };

        this.RenderPage = function (pPageView, retryCount) {
            if (pPageView == null || !pPageView.IsContentCreated())
                return;
            if (pPageView.IsPageLoaded()) {
                return;
            }
            var thumbStr = "noThumb";
            if (pPageView.IsThumb())
                thumbStr = "thumb";
            var pageKey = thumbStr + pPageView.GetPageIndex();
            if (retryCount <= 0) {
                FXREADER.Logger.info("Maximum number of retries exceeded for render page: " + pPageView.GetPageIndex() + "; IsThumb:" + pPageView.IsThumb());
                _iCurPageNum = -1;
                _PageInRenderList[pageKey] = null;
                return;
            }
            //以下具体的显示流程;
            // 当前有正在处理的页面;
            if (_PageInRenderList[pageKey] == pPageView) {
                return;
            }
            else if (_iCurPageNum > -1) {
                   setTimeout(function () {
                	   _self.RenderPage(pPageView, retryCount);
                    }, 500);
            }
            else {
            	var validPageOffset = 1;
            	var displayRange = null;
            	if (pPageView.IsThumb())
            	{
            		var pThumbsView = pPageView.GetThumbsView();
            		displayRange = pThumbsView.GetDisplayPageRange();
            	}
            	else
            	{
            		var pDocView = pPageView.GetDocView();
            		displayRange = pDocView.GetVisablePageRange();
            	}
            	if(pPageView.GetPageIndex()<=displayRange.begin-validPageOffset 
            			|| pPageView.GetPageIndex()>=displayRange.end+validPageOffset)
            	{
            		return;
            	}
            	
                _iCurPageNum = pPageView.GetPageIndex();
                _PageInRenderList[pageKey] = pPageView;
                var imgurl = _self.GetPageImgUrl(_iCurPageNum, pPageView.GetScale(), pPageView.IsThumb());
                new CImgLazyLoad(imgurl,pPageView, function (curPageView,state) {
                    var pReaderApp = FXREADER.AppInstance;
                    var bLoadError = false;
                    var bGetPageImgFailed = (state==FXREADER.PDFView.GetImageErrorCode.ERROR_CREATE_IMG_FAILED);
                    var bPageImgLimit = (state==FXREADER.PDFView.GetImageErrorCode.ERROR_PAGE_DISPLAY_LIMIT);
                    var isInProgress = (state==FXREADER.PDFView.GetImageErrorCode.ERROR_INPROGRESS);
                	if(bGetPageImgFailed || bPageImgLimit)
                    {
                    	bLoadError = true;
                    }
                	else if (isInProgress) {
                          if(_maxRenderPageIndex == -1)
                        	  _maxRenderPageIndex = 0;
                          var pDocView = FXREADER.AppInstance.GetMainView().GetDocView();
                          var interval = FXREADER.AjaxRetryManager.GetNextPageImageRequestInterval(_iCurPageNum,_maxRenderPageIndex,
                        		  pDocView.GetPageCount());
                          _iCurPageNum = -1;
                          _PageInRenderList[pageKey] = null;
                          setTimeout(function () { _self.RenderPage(curPageView, retryCount - 1); }, interval);
                          return;
                    }
                	_maxRenderPageIndex = pPageView.GetPageIndex();
                	curPageView.SetPageLoaded(true);
                	if(!bLoadError)
                    {
                        $("#" + curPageView.GetPageBGImgID()).attr("src", imgurl);
                        curPageView.Show();
                    }
                	else
                	{
                		var failedImgUrl = "";
                		if(bGetPageImgFailed)
                		   failedImgUrl = "images/imgFailed.png";
                		else
                		   failedImgUrl = "images/imgLimit.png";
                		curPageView.ShowErrorPage(failedImgUrl);
                	}
                    if(!curPageView.IsThumb())
                    {
                    	
                    	curPageView.SetPageLoadError(bLoadError);
                    	pReaderApp.OnPageShowComplete(curPageView);
                    }
                    _iCurPageNum = -1;
                    _PageInRenderList[pageKey] = null;
                }, function (curPageView) {
                	   _iCurPageNum = -1;
                       _PageInRenderList[pageKey] = null;
                       curPageView.SetPageLoaded(true);
                       curPageView.ShowErrorPage("images/imgFailed.png");
                       if(!curPageView.IsThumb())
                       {
                    	   curPageView.SetPageLoadError(true);
                       		pReaderApp.OnPageShowComplete(curPageView);
                       }
                       return;
                });
            }

        };

        this.GetPixelsPerPoint = function () {
            return 96 / 72;
        };
    };
    return FXREADER.PDFView.CImgPageViewRender;

});